Namespace PL.MetaSDK
    Public Class MetaApplicationOptions_PL
        Inherits PLBase

        Public Sub New(ByVal queryName As String)
            MyBase.New(queryName)
        End Sub

        Public Overrides Function Save() As Boolean
            Try
                Me.MetaDataTable.ApplyChanges()
                Return True
            Catch sqlex As System.Data.SqlClient.SqlException
                'HACK: On Utilise les codes de SQL. Attention si les codes change entre ADO et/ou SQL
                Select Case sqlex.Number
                    Case 2601, 2627  'Unique Index/Constriant Violation
                        Throw New Exception("Record Already Exist, Operation Aborded", sqlex)
                    Case Else
                        Throw New Exception(String.Format("Database Error: {0} #{1} ", sqlex.Message, sqlex.Number), sqlex)
                End Select
            Catch dbcex As System.Data.DBConcurrencyException
                Throw New Exception("Concurrency Issue, Record Already Modified", dbcex)
            Catch ex As Exception
                Throw New Exception(String.Format("Row Cannot Be Save: {0}. Make Sure You Have a CUD Command", ex.Message), ex)
            End Try
            Return False
        End Function

        ''' <summary>
        ''' Cette procedure est appellé pour chaque ligne qui sera traité.
        ''' quand le Me.MetaDataTable.Adapter.Update() est appelé
        ''' Si on veut faire de la logique sur les lignes il faut vérifier le 
        ''' DataRowState.Added, DataRowState.Modified etc...
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Sub OnRowUpdating(ByVal sender As Object, ByVal e As SqlRowUpdatingEventArgs)

        End Sub

        Public Overrides Function Delete() As Boolean
            'Throw New NotImplementedException("Delete To Come...")
            Return False
        End Function

        Public Overrides Function InitData(Optional ByVal withChild As Boolean = True) As Boolean
            Try
                Me.DataSet = New DataSet()
                MetaDataTable = New MetaDataTable()
                Me.MetaDataTable.QueryName = Me.QueryName
                Me.MetaDataTable.GetMetaCommand()
                Me.MetaDataTable.MetaCommand.SQLFrom += String.Format(" and MetaUserOptions.UserId_FK = '{0}'", SCIEF.ActiveFramework.CommonExt.Base.AppManager.UserID)
                Me.MetaDataTable.FillBusinessApplicationData()
                Me.DataSet.Tables.Add(Me.MetaDataTable)
                'OutStandingIssue: a snippetter = aleblanc
                If withChild Then
                    Dim aParentRelationField As String = Me.MetaDataTable.MetaCommand.GUIDField
                    For Each row As DataRow In Me.MetaDataTable.MetaCommand.GetChildDisplayQuery.Rows
                        Dim metaDataTableChild As CommonExt.MetaDataTable = New CommonExt.MetaDataTable() With {.QueryName = row("QueryName")}
                        metaDataTableChild.FillBusinessApplicationData()
                        Me.DataSet.Tables.Add(metaDataTableChild)
                        Dim aChildRelationField As String = Me.MetaDataTable.MetaCommand.GUIDField & "_FK"
                        Dim aDR As New DataRelation(row("Relation").ToString, Me.MetaDataTable.Columns(aParentRelationField), metaDataTableChild.Columns(aChildRelationField))
                        Me.DataSet.Relations.Add(aDR)
                    Next
                End If
                Return True
            Catch ex As Exception
                Throw ex
            End Try
            Return False
        End Function

        Public Overrides Function Validate() As Boolean
            'Throw New NotImplementedException("Validate To Come...")
            Return True
        End Function
    End Class
End Namespace
